<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .container {
            width: 600px;
            height: 600px;
            border: 10px solid pink;
            border-radius: 10px;
            margin: 0 auto;
            position: relative;
        }

        .container div {
            width: 30%;
            height: 30%;
            background: gray;
            position: absolute;
            font-size: 50px;
            color: white;
            display: flex;
            justify-content: center;
            align-items: center;
            border-radius: 5px;
        }
    </style>
</head>

<body>
    <div class="container">
        <div style="left:2%;top:2%">1</div>
        <div style="left:35%;top:2%">2</div>
        <div style="left:68%;top:2%">3</div>
        <div style="left:2%;top:35%">4</div>
        <div style="left:35%;top:35%">5</div>
        <div style="left:68%;top:35%">6</div>
        <div style="left:2%;top:68%">7</div>
        <div style="left:35%;top:68%">8</div>
        <div style="left:68%;top:68%">9</div>
    </div>
    <script>
        var arr = ['red', 'blue', 'yellow', 'orange', 'black', 'pink', 'gray', 'purple', 'green'];
        let divs = document.querySelectorAll(".container div");
        divs.forEach(function (div, key) {
            div.style.backgroundColor = arr[key];
        })


        var containerEle = document.querySelector(".container");
        // 实现多个元素的拖动 
        divs.forEach(function (item) {
            item.onmousedown = function (event) {
                //    开始的时候清除过渡效果；
                this.style.transition = "none";
                // 开始拖动的时候
                var obj = {};  //记录开始拖动的div的left和top的值；
                var maxEle;
                obj.left = this.style.left;
                obj.top = this.style.top;
                this.style.zIndex = 10;
                var e = event || window.event;
                var x = e.clientX - this.offsetLeft;
                var y = e.clientY - this.offsetTop;
                this.onmousemove = function (event) {
                    event.preventDefault();
                    var e = event || window.event;
                    var xx = e.clientX;
                    var yy = e.clientY;

                    // 限定方块的拖拽范围
                    var xnum = xx - x;
                    var ynum = yy - y;

                    // 左边的范围
                    if(xnum<=0){
                        xnum =  0;
                    }

                    // 上边的边距
                    if(ynum<=0){
                        ynum = 0;
                    }

                    // 右边边距
                    // containerEle.offsetWidth  containerEle.clientWidth;
                    if(xnum>=(containerEle.clientWidth-this.offsetWidth)){
                        xnum = containerEle.clientWidth-this.offsetWidth
                    }


                    // 下边距

                    if(ynum>=(containerEle.clientHeight-this.offsetHeight)){
                        ynum = containerEle.clientHeight-this.offsetHeight
                    }

                    this.style.left = xnum + "px";
                    this.style.top = ynum + "px";

                    var crashArr = []; // 保存被碰撞了元素；
                    // 检测元素的碰撞 ： 除了拖动元素之外的所有的其他元素检测碰撞；
                    var that = this;
                    divs.forEach(function (item) {
                        // 不能检测自己和自己的碰撞；
                        if (that !== item) {
                            // 碰撞检测
                            if (
                                Math.abs(that.offsetLeft - item.offsetLeft) < that.offsetWidth &&
                                Math.abs(that.offsetTop - item.offsetTop) < that.offsetHeight
                            ) {
                                crashArr.push(item);
                            }
                        }
                    })
                    console.log(crashArr);

                    // 求碰撞的面积
                    var maxArae = 0;


                    crashArr.forEach(function (item) {
                        // var w = Math.abs( that.offsetLeft + that.offsetWidth - item.offsetLeft);
                        // var h =   Math.abs( that.offsetTop + that.offsetHeight - item.offsetTop);

                        var w = that.offsetWidth - Math.abs(that.offsetLeft - item.offsetLeft);
                        var h = that.offsetHeight - Math.abs(that.offsetTop - item.offsetTop);
                        // console.log(h);
                        var arae = w * h;
                        // console.log(arae);
                        if (arae > maxArae) {
                            maxArae = arae;
                            maxEle = item;
                        }
                    })
                    // console.log(maxEle);

                }
                containerEle.onmouseup = function () {
                    // 拖动结束；
                    item.onmousemove = "";
                    item.style.zIndex = 0;
                    // console.log(obj);
                    // 把div还原 到初始位置；
                    if (typeof maxEle !== "undefined") {
                        item.style.left = maxEle.style.left;
                        item.style.top = maxEle.style.top;
                        maxEle.style.transition = "all 1s";
                        maxEle.style.left = obj.left;
                        maxEle.style.top = obj.top;
                        item.style.transition = "all 1s";
                    }

                }
            }
        })

        // 1.百度全选 2.拖动边距控制 3.拖动的思路 4.预习


    </script>
</body>

</html>